package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/system/userModel")
public class SysUserModelController  extends BaseController {
    @Autowired
    private ISysUserModelService userModelService;
    @Autowired
    private UserDataSourceService userDataSourceService;
    @Autowired
    private UserVisualBaseService userVisualBaseService;
    @Autowired
    private DataModuleService dataModuleService;
    @Autowired
    private ISysModelManageService modelManageService;


    private String prefix = "system/userModel";

    @RequiresPermissions("system:userModel:view")
    @GetMapping()
    public String modelType(@Validated String baseId, @Validated Integer moduleId,  @Validated VisualModule visualModule, ModelMap map)
    {
        if(baseId!=null && baseId.trim()!="")
            visualModule.setBaseId(Integer.valueOf(baseId));
        if(moduleId!=null && moduleId!=0){
            visualModule.setModuleId(moduleId);
        }

        map.put("visualModule",visualModule);
        List<DataModule> dataModules = dataModuleService.selectDataModuleListByInfo(new DataModule());
        map.put("dataModules",dataModules);

        return prefix + "/userModel";
    }

    /**
     * 根据条件查询用户大屏元素
     * @param visualModule
     * @return
     */
    @RequiresPermissions("system:userModel:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VisualModule visualModule,@Validated String moduleIds)
    {

        startPage();
        List<VisualModule> visualModules = userModelService.selectAllUserModel(visualModule,moduleIds);
        return getDataTable(visualModules);
    }

    /**
     * 根据ids逻辑删除用户大屏元素
     * @param ids
     * @return
     */
    @RequiresPermissions("system:userModel:remove")
    @Log(title = "用户大屏元素",businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        return toAjax(userModelService.deleteUserModelByIds(Convert.toStrArray(ids)));
    }



    /**
     * 修改大屏元素信息
     */
    @RequiresPermissions("system:userModel:edit")
    @Log(title = "大屏元素", businessType = BusinessType.UPDATE)
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        mmap.put("user",user);
        VisualModule visualModule = userModelService.selectUserModelById(Integer.valueOf(id));
        mmap.put("visualModule",visualModule);
        //把用户ID包装进数据源对象中当作参数
        List<UserDataSource> userDataSources = userDataSourceService.selectDataSourceAll(new UserDataSource(Integer.valueOf(visualModule.getCreateId())));
        mmap.put("userDataSources",userDataSources);

        UserVisualBase userVisualBase = new UserVisualBase();
        userVisualBase.setUserId(Integer.valueOf(user.getUserId().toString()));
        //获取该用户的所有数据大屏
        List<UserVisualBase> userVisualBases = userVisualBaseService.selectUserVisualBaseListByInfo(userVisualBase);
        mmap.put("userVisualBases",userVisualBases);

        return prefix + "/edit";
    }

    /**
     * 修改大屏元素
     * @param visualModule
     * @return
     */
    @RequiresPermissions("system:modekType:edit")
    @Log(title = "大屏元素管理",businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@Validated VisualModule visualModule){

        visualModule.setUpdateId(ShiroUtils.getSysUser().getUserId().toString());
        visualModule.setUpdateName(ShiroUtils.getSysUser().getLoginName());

        return toAjax(userModelService.updateUserModelById(visualModule));

    }


    /**
     * 修改大屏元素数据
     * @param visualId
     * @param map
     * @return
     */
    @RequiresPermissions("system:userModel:edit")
    @Log(title = "大屏元素数据", businessType = BusinessType.UPDATE)
    @GetMapping("/editDataRule/{visualId}")
    public String updataDataRule(@PathVariable("visualId") Integer visualId,ModelMap map){

        List<DataVisualRule> dataVisualRules = userModelService.selectDataRuleByVisualId(visualId);
        map.put("dataVisualRules",dataVisualRules);
        return prefix + "/editDataRule";
    }

    /**
     * 修改大屏元素数据
     * @param id
     * @param keyCode
     * @param valueCode
     * @return
     */
    @RequiresPermissions("system:modekType:editData")
    @Log(title = "大屏元素数据",businessType = BusinessType.UPDATE)
    @PostMapping("/editData")
    @ResponseBody
    public AjaxResult editData(@Validated String id,@Validated String keyCode,@Validated String valueCode){

        List<DataVisualRule> list = new ArrayList<DataVisualRule>();

        String[] ids = id.substring(1).split(",");
        String[] keyCodes = keyCode.substring(1).split(",");
        String[] valueCodes = valueCode.substring(1).split(",");
        DataVisualRule dataVisualRule;
        for(int i = 0;i<=ids.length-1;i++){
            dataVisualRule = new DataVisualRule(Integer.valueOf(ids[i]),keyCodes[i],valueCodes[i]);
            list.add(dataVisualRule);
        }

        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        for(int i = 0;i<=list.size()-1;i++){
            list.get(i).setUpdateId(user.getUserId().toString());
            list.get(i).setUpdateName(user.getUserName());
        }
        int i = userModelService.updataDataRuleById(list);
        return toAjax(i);
        //未测试
    }


    /**
     * 查看样式JSON
     */
    @GetMapping("/showCssJson/{id}")
    public String showCssJson(@PathVariable("id") String id, ModelMap map)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        map.put("user",user);
        VisualModule visualModule = userModelService.selectUserModelById(Integer.valueOf(id));
        map.put("cssStyle", visualModule.getCssStyle());
        return prefix + "/showCssJson";
    }

    @GetMapping("/showDataModel")
    public String showDataModel(ModelMap modelMap){
        List<DataModuleType> moduleTypes = modelManageService.selectAllModelType(null);
        modelMap.put("moduleTypes",moduleTypes);
        return prefix + "/showDataModel";
    }


}
